<?php

class AdministracaoService extends Model {

    /**
     * @var AdministracaoDao
     */
    private $administracaoDao;
    /**
     * @var FuncaoService
     */
    private $funcaoService;
    /**
     * @var DepartamentoService
     */
    private $departamentoService;
    /**
     * @var SetorService
     */
    private $setorService;
    /**
     * @var UsuarioService
     */
    private $usuarioService;

    const EMPLOYEE_ALL = 'list-all';
    const EMPLOYEE_ACTIVE = 'list-active';
    const PENDING_EMPLOYEE = 'list-pending-user';
    const TIMELY_EMPLOYEE = 'list-timely-payment-user';

    public function injectDependencies() {
        $this->administracaoDao = AdministracaoDao::getInstance();
        $this->funcaoService = FuncaoService::getInstance();
        $this->departamentoService = DepartamentoService::getInstance();
        $this->setorService = SetorService::getInstance();
        $this->usuarioService = UsuarioService::getInstance();
    }

    public function findUserByFilter($IDFilter, $filterType) {
        if( $filterType == 'employee' ) {
            if ($IDFilter == self::EMPLOYEE_ALL) {
                return $this->usuarioService->findAll();
            } else if ($IDFilter == self::EMPLOYEE_ACTIVE) {
                return $this->usuarioService->findByStatus('ATIVADO');
            }
        } else if( $filterType == 'employee-payment' ) {
            if( $IDFilter == self::PENDING_EMPLOYEE ) {
                return $this->usuarioService->findUsersWithPendingPayment();
            } else if( $IDFilter == self::TIMELY_EMPLOYEE ) {
                return $this->usuarioService->findUsersInTimelyPayment();
            }
        }
        return false;
    }

    //07-04
    public function findEmployeeById($IDUsuario) {
        return $this->administracaoDao->findUserById($IDUsuario);
    }

    public function updateEmployee($ArDados) {
        return $this->administracaoDao->updateEmployee($ArDados);
    }

    public function saveEmployee($ArDados) {
        return $this->administracaoDao->saveEmployee($ArDados);
    }

    public function getGeneralSalarialSheet() {
        $Report = array(
            'TotalEnterprise' => $this->getTotalSalarialSheet(),
            'EmployeeQuantity' => $this->getNumberOfEmployees(),
        );
        $Report['Average'] = $Report['TotalEnterprise']/$Report['EmployeeQuantity'];
        $Report['Percentage'] = '100';
        return $Report;
    }

    public function getTotalSalarialSheet() {
        return $this->administracaoDao->getTotalSalarialSheet();
    }

    public function getNumberOfEmployees() {
        return $this->administracaoDao->getNumberOfEmployees();
    }

    public function getPositionSalarialSheet() {
        $Report = array();
        $totalSalarialSheet = $this->getTotalSalarialSheet();
        $ArPositions = $this->funcaoService->findAll();
        foreach($ArPositions as $Position) {
            $Report[] = array(
                'PositionName' => $Position->getStNome(),
                'TotalPosition' => $this->getTotalSheetByPosition($Position),
                'EmployeeQuantity' => $Position->countContractorsEmployees()
            );
        }
        foreach($Report as &$line) {
            $line['Average'] = MathUtils::division($line['TotalPosition'], $line['EmployeeQuantity'], 0);
            $line['Percentage'] = MathUtils::getPercent($totalSalarialSheet, $line['TotalPosition']);
        }
        return $Report;
    }

    public function getSectorSalarialSheet() {
        $Report = array();
        $totalSalarialSheet = $this->getTotalSalarialSheet();
        $ArSectors = $this->setorService->findAll();

        foreach($ArSectors as $Sector) {
            $Sector->loadEmployee();
            $Report[] = array(
                'SectorName' => $Sector->getStNome(),
                'TotalSector' => $this->administracaoDao->getTotalSheetBySector($Sector),
                'EmployeeQuantity' => $Sector->countContractorEmployees()
            );
        }

        foreach($Report as &$line) {
            $line['Average'] = MathUtils::division($line['TotalSector'], $line['EmployeeQuantity'], 0);
            $line['Percentage'] = MathUtils::getPercent($totalSalarialSheet, $line['TotalSector']);
        }

        return $Report;
    }

    public function getDepartamentSalarialSheet() {
        $Report = array();
        $totalSalarialSheet = $this->getTotalSalarialSheet();
        $ArDepartaments = $this->departamentoService->findAll();

        foreach($ArDepartaments as $Departament) {
            $Report[] = array(
                'DepartamentName' => $Departament->getStNome(),
                'TotalDepartament' => $this->administracaoDao->getTotalSheetByDepartament($Departament),
                'EmployeeQuantity' => $Departament->countEmployees()
            );
        }
        foreach($Report as &$line) {
            $line['Average'] = MathUtils::division($line['TotalDepartament'], $line['EmployeeQuantity'], 0);
            $line['Percentage'] = MathUtils::getPercent($totalSalarialSheet, $line['TotalDepartament']);
        }

        return $Report;
    }

    public function getTotalSheetByPosition($Position) {
        return $this->administracaoDao->getTotalSheetByPosition($Position);
    }

    public function getLastFiredEmployees() {
        $Report = array();
        $lastFiredEmployees = $this->administracaoDao->getLastFiredEmployees();
        foreach($lastFiredEmployees as $Usuario) {
            $Report[] = array(
                'StNome' => $Usuario->getArDadosUsuario('StNome'),
                'DtAdmissao' => dateBDtoBR($Usuario->getArDadosAdministracao('DtAdmissao')),
                'StCargo' => $Usuario->getTPerfil()->getFuncao()->getStNome(),
                'FoSalario' => MathUtils::money($Usuario->getArDadosAdministracao('FoSalario')),
            );
        }
        return $Report;
    }

    public function getLastRegisteredEmployees() {
        $Report = array();
        $lastRegisteredEmployees = $this->administracaoDao->getLastRegisteredEmployees();
        foreach($lastRegisteredEmployees as $Usuario) {
            $Report[] = array(
                'StNome' => $Usuario->getArDadosUsuario('StNome'),
                'DtAdmissao' => dateBDtoBR($Usuario->getArDadosAdministracao('DtAdmissao')),
                'StCargo' => $Usuario->getTPerfil()->getFuncao()->getStNome(),
                'FoSalario' => MathUtils::money($Usuario->getArDadosAdministracao('FoSalario')),
            );
        }
        return $Report;
    }

    public function hasReservistCertifiedRegistered($StCertificadoReservista) {
        return $this->administracaoDao->haReservistCertifiedRegistered($StCertificadoReservista);
    }

    public function hasPisRegistered($StPis) {
        return $this->administracaoDao->hasPisRegistered($StPis);
    }

    public function hasElectorTitleRegistered($StTituloEleitor) {
        return $this->administracaoDao->hasElectorTitleRegistered($StTituloEleitor);
    }

}

?>
